package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;

import com.hctforgreen.greenservice.LoginActivity;
import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.TipsEntity;
import com.hctforgreen.greenservice.utils.LoginInStoreUtil;
/**
 * 提示信息的适配器
 * @author Tallen Zhai
 *
 */
public class TipsAdapter extends BaseAdapter {

	private List<TipsEntity> mEntities;
	private Activity mActivity;
	private LayoutInflater mInflater;

	public TipsAdapter(Activity activity, List<TipsEntity> entities) {
		mActivity = activity;
		mEntities = entities;
		mInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_tips_lst, null);
		}

		final TipsEntity entity = (TipsEntity) getItem(position);
		final ImageView tipsIv = (ImageView) convertView
				.findViewById(R.id.iv_tips);
		tipsIv.setImageResource(entity.drawable_id);

		final Button startBtn = (Button) convertView
				.findViewById(R.id.btn_start);
		startBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent();
				i.setClass(mActivity, LoginActivity.class);
				mActivity.startActivity(i);
				LoginInStoreUtil.saveFirstInstall(mActivity, false);
				mActivity.finish();
			}
		});
		if(position == mEntities.size() - 1){
			startBtn.setVisibility(View.VISIBLE);
		}else{
			startBtn.setVisibility(View.GONE);
		}
		return convertView;
	}

}
